package com.pandora.credit.mongo.service.impl;

import com.pandora.common.util.JsonUtil;
import com.pandora.common.util.MagicConst;
import com.pandora.credit.exception.InvalidResponseException;
import com.pandora.credit.mongo.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author developer
 */
public class ConvertReportFormatFactory {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private ConvertReportFormatFactory() {
    }

    private static ConvertReportFormatFactory single = null;

    public static ConvertReportFormatFactory getInstance() {
        if (single == null) {
            synchronized (ConvertReportFormatFactory.class) {
                if (single == null) {
                    single = new ConvertReportFormatFactory();
                }
            }
        }
        return single;
    }

    public AbstractReportAfterFormat createReportFormat(int reportTypeCode) {
        //OP
        if (reportTypeCode == MagicConst.NUM_0) {
            return new OpConvertReportFormat();
        }
        //TR
        if (reportTypeCode == MagicConst.NUM_1) {
            return new TrConvertReportFormat();
        }
        //WM
        if (reportTypeCode == MagicConst.NUM_4) {
            return new WmConvertReportFormat();
        }
        return null;
    }

    public AbstractReport getReportObject(String type, String report) {
        try{
            //OP
            if (MagicConst.REPORT_TYPE_OP.equals(type)) {
                return JsonUtil.convertStringToBean(report, OpReport.class);
            }
            //TR
            if (MagicConst.REPORT_TYPE_TR.equals(type)) {
                return JsonUtil.convertStringToBean(report, TrReport.class);
            }
            //WM
            if (MagicConst.REPORT_TYPE_WM.equals(type)) {
                return JsonUtil.convertStringToBean(report, WmReport.class);
            }
        } catch(Exception e) {
            logger.error("json文件不能转成对象，请检查文件！");
            throw new InvalidResponseException();
        }
        return null;
    }
}
